package MediaOnline.General;

import java.security.MessageDigest;

public class stringMD5 {
	
	static public String ToString(byte[] b)
	{
		String s = "";
		for (int i = 0; i < b.length; i++)
		{
			if (i != 0 && i % 4 == 0)
			{
				s += "-";
			}
			s += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1).toUpperCase();
		}
		return s;
	}
	
	static public byte[] Parse(String s)
	{
		s = s.replace("-", "");
		byte[] b = new byte[s.length()/2];
		String sub;
		for (int i = 0; i < b.length; i++)
		{
			sub = s.substring(i * 2, i * 2 + 1);
			b[i] = (byte)Integer.parseInt(sub, 16);
		}
		return b;		
	}
	
	static public String ToMD5(String s)
	{
		try
		{
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] desString = md.digest(s.getBytes("UTF-8"));
			return ToString(desString);			
		}catch (Exception e)
		{
			return e.getMessage();
		}				
	}
	
}
